package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.mapper.ext.ExtAmInterestAccrualInvestorInfoMapper;
import com.jrx.anytxn.investor.mapper.ext.ExtAmLoanAccountInvestorInfoMapper;
import com.jrx.anytxn.investor.mapper.ext.ExtAmRepaymentPlanInvestorInfoMapper;
import com.jrx.anytxn.investor.mapper.ext.ExtTlPaymentAllocationInvestorLogMapper;
import com.jrx.anytxn.investor.service.IInvestorFileService;
import com.jrx.anytxn.param.dto.investor.FileBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-transaction
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-06-09 09:51
 **/
@Service
public class InvestorFileServiceImpl implements IInvestorFileService {

    private final Logger logger = LoggerFactory.getLogger(InvestorFileServiceImpl.class);


    @Resource
    private ExtAmRepaymentPlanInvestorInfoMapper extAmRepaymentPlanInvestorInfoMapper;
    @Resource
    private ExtAmInterestAccrualInvestorInfoMapper extAmInterestAccrualInvestorInfoMapper;
    @Resource
    private ExtAmLoanAccountInvestorInfoMapper extAmLoanAccountInvestorInfoMapper;
    @Resource
    private ExtTlPaymentAllocationInvestorLogMapper extTlPaymentAllocationInvestorLogMapper;



    @Override
    public FileBean createOrderFileMessage(AmLoanAccountInvestorInfo info) {
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
        sb.append(info.getChannel() == null ? "" : info.getChannel()).append(TransBizConstant.separator);
        sb.append(info.getAccountStatus() == null ? "" : info.getAccountStatus()).append(TransBizConstant.separator);
        sb.append(info.getProductId() == null ? "" : info.getProductId()).append(TransBizConstant.separator);
        sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
        sb.append(info.getRemainPrincipal() == null ? "0.00" : info.getRemainPrincipal().toPlainString()).append(TransBizConstant.separator);
        BigDecimal interestAmt = info.getInterestBalance().add(info.getAccruedInterest());
        sb.append(interestAmt == null ? "0.00" : interestAmt.toPlainString()).append(TransBizConstant.separator);
        BigDecimal pentyAmt = info.getPenaltyBalance().add(info.getAccruedPenalty());
        sb.append(pentyAmt == null ? "0.00" : pentyAmt.toPlainString()).append(TransBizConstant.separator);
        BigDecimal compAmt = info.getCompoundBalance().add(info.getAccruedCompound());
        sb.append(compAmt == null ? "0.00" : compAmt.toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getFeeBalance() == null ? "0.00" : info.getFeeBalance().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getPayedTenor() == null ? "0" : info.getPayedTenor()).append(TransBizConstant.separator);
        sb.append(info.getChargeoffFlag() == null ? "" : info.getChargeoffFlag()).append(TransBizConstant.separator);
        sb.append(info.getChargeoffAmount() == null ? "0.00" : info.getChargeoffAmount().toPlainString()).append(TransBizConstant.separator);
        String chargeoffDate = info.getChargeoffDate() == null ? "" : DateUtils.formatDate(info.getChargeoffDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(chargeoffDate).append(TransBizConstant.separator);
        sb.append(info.getChargeoffReasonCode() == null ? "" : info.getChargeoffReasonCode()).append(TransBizConstant.separator);
        sb.append(info.getWaiveInterestFlag() == null ? "" : info.getWaiveInterestFlag()).append(TransBizConstant.separator);
        sb.append(info.getWaiveFeeFlag() == null ? "" : info.getWaiveFeeFlag()).append(TransBizConstant.separator);
        sb.append(info.getContinueOverDueTenor() == null ? "0" : info.getContinueOverDueTenor()).append(TransBizConstant.separator);
        sb.append(info.getTotalOverDueTenor() == null ? "0" : info.getTotalOverDueTenor()).append(TransBizConstant.separator);
        sb.append(info.getAccountingPhase() == null ? "" : info.getAccountingPhase()).append(TransBizConstant.separator);
        sb.append(info.getNonAccrualsFlag() == null ? "" : info.getNonAccrualsFlag()).append(TransBizConstant.separator);
        sb.append(info.getDelqDays() == null ? "0" : info.getDelqDays()).append(TransBizConstant.separator);
        sb.append(info.getLoanClassify() == null ? "" : info.getLoanClassify()).append(TransBizConstant.separator);
        sb.append(info.getPayoffDate() == null ? "" : info.getPayoffDate()).append(TransBizConstant.separator);
        sb.append( "\r\n");
        fileBean.setInvestorId(info.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createPlanFileMessage(AmLoanAccountInvestorInfo loanAccount) {
        List<AmRepaymentPlanInvestorInfo> planInvestorInfoList = extAmRepaymentPlanInvestorInfoMapper.selectByInvestorLoanIdNotPlanStatus
                (loanAccount.getInvestorLoanId(), TransBizConstant.LOAN_PLAN_STATUS_9);
        List<AmInterestAccrualInvestorInfo> accuAcctList = extAmInterestAccrualInvestorInfoMapper.selectByInvestorLoanId(loanAccount.getInvestorLoanId());

        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        planInvestorInfoList.forEach(
                info -> {
                    sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
                    sb.append(info.getInvestorId() == null ? "" : info.getInvestorId()).append(TransBizConstant.separator);
                    sb.append(info.getBillingTenor() == null ? "" : info.getBillingTenor()).append(TransBizConstant.separator);
                    sb.append(info.getChannel() == null ? "" : info.getChannel()).append(TransBizConstant.separator);
                    sb.append(info.getPlanStatus() == null ? "" : info.getPlanStatus()).append(TransBizConstant.separator);
                    sb.append(info.getProductId() == null ? "" : info.getProductId()).append(TransBizConstant.separator);
                    sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
                    sb.append(info.getPrincipalAmount() == null ? "0.00" : info.getPrincipalAmount().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getInterestAmount() == null ? "0.00" : info.getInterestAmount().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getFeeAmount() == null ? "0.00" : info.getFeeAmount().toPlainString()).append(TransBizConstant.separator);
                    BigDecimal pentyAmt = accuAcctList.stream().filter(acct -> info.getBillingTenor().equals(acct.getBillingTenor()))
                            .filter(acct -> TransBizConstant.INTEREST_ACCRUAL_TYPE_10.equals(acct.getAccountType()))
                            .map(AmInterestAccrualInvestorInfo::getAccrualTotalAccruInterest).reduce(BigDecimal.ZERO, BigDecimal::add);
                    sb.append(pentyAmt == null ? "0.00" : pentyAmt.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal compAmt = accuAcctList.stream().filter(acct -> info.getBillingTenor().equals(acct.getBillingTenor()))
                            .filter(acct -> TransBizConstant.INTEREST_ACCRUAL_TYPE_20.equals(acct.getAccountType()))
                            .map(AmInterestAccrualInvestorInfo::getAccrualTotalAccruInterest).reduce(BigDecimal.ZERO, BigDecimal::add);
                    sb.append(compAmt == null ? "0.00" : compAmt.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal principalBalance = info.getPrincipalAmount().subtract(info.getRepayPrincipal());
                    sb.append(principalBalance == null ? "0.00" : principalBalance.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal interestBalance = info.getInterestAmount().subtract(info.getRepayInterest());
                    sb.append(interestBalance == null ? "0.00" : interestBalance.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal feeBalance = info.getFeeAmount().subtract(info.getRepayFee());
                    sb.append(feeBalance == null ? "0.00" : feeBalance.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal penaltyBalance = pentyAmt.subtract(info.getRepayPenaltyInterest());
                    sb.append(penaltyBalance == null ? "0.00" : penaltyBalance.toPlainString()).append(TransBizConstant.separator);
                    BigDecimal compBalance = compAmt.subtract(info.getRepayCompoundInterest());
                    sb.append(compBalance == null ? "0.00" : compBalance.toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getRepayPrincipal() == null ? "0.00" : info.getRepayPrincipal().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getRepayInterest() == null ? "0.00" : info.getRepayInterest().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getRepayPenaltyInterest() == null ? "0.00" : info.getRepayPenaltyInterest().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getRepayCompoundInterest() == null ? "0.00" : info.getRepayCompoundInterest().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getRepayFee() == null ? "0.00" : info.getRepayFee().toPlainString()).append(TransBizConstant.separator);
                    String paymentDueDate = info.getPaymentDueDate() == null ? "" : DateUtils.formatDate(info.getPaymentDueDate(), DateUtils.DATE_PATTERN_ANOTHER);
                    sb.append(paymentDueDate).append(TransBizConstant.separator);
                    String transactionDate = info.getTransactionDate() == null ? "" : DateUtils.formatDate(info.getTransactionDate(), DateUtils.DATE_PATTERN_ANOTHER);
                    sb.append(transactionDate).append(TransBizConstant.separator);
                    String payoffDate = info.getPayoffDate() == null ? "" : DateUtils.formatDate(info.getPayoffDate(), DateUtils.DATE_PATTERN_ANOTHER);
                    sb.append(payoffDate).append(TransBizConstant.separator);
                    sb.append(info.getDelqDays() == null ? "0" : info.getDelqDays()).append(TransBizConstant.separator);
                    sb.append(info.getChargeoffFlag() == null ? "" : info.getChargeoffFlag()).append(TransBizConstant.separator);
                    sb.append( "\r\n");
                }
        );
        fileBean.setInvestorId(loanAccount.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createLoanOrderFileMessage(AmLoanMainInvestorInfo info) {
        AmLoanAccountInvestorInfo loanAccount = extAmLoanAccountInvestorInfoMapper.selectByInvestorLoanIdAndCustId(info.getInvestorLoanId(),
                info.getCustomerId(), info.getTenantId());
        if (TransBizConstant.LOAN_STATUS_9.equals(loanAccount.getAccountStatus()) || TransBizConstant.LOAN_STATUS_0.equals(loanAccount.getAccountStatus())) {
            return null;
        }

        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
        sb.append(info.getInvestorId() == null ? "" : info.getInvestorId()).append(TransBizConstant.separator);
        sb.append(info.getInvertorPercentage() == null ? "0.00" : info.getInvertorPercentage().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getChannel() == null ? "" : info.getChannel()).append(TransBizConstant.separator);
        sb.append(info.getProductId() == null ? "" : info.getProductId()).append(TransBizConstant.separator);
        sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
        sb.append(info.getLoanAmount() == null ? "0.00" : info.getLoanAmount().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getInterestRate() == null ? "0.00" : info.getInterestRate().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getActualDailyRate() == null ? "0.00" : info.getActualDailyRate().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getPaymentDateDd() == null ? "" : info.getPaymentDateDd()).append(TransBizConstant.separator);
        String interestStartDate = info.getInterestStartDate() == null ? "" : DateUtils.formatDate(info.getInterestStartDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(interestStartDate).append(TransBizConstant.separator);
        sb.append(info.getTotalTenor() == null ? "" : info.getTotalTenor()).append(TransBizConstant.separator);
        String txnTime = info.getTxnTime() == null ? "" : DateUtils.formatDate(info.getTxnTime(), DateUtils.DATETIME_PATTERN);
        sb.append(txnTime).append(TransBizConstant.separator);
        String postingDate = info.getPostingDate() == null ? "" : DateUtils.formatDate(info.getPostingDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(postingDate).append(TransBizConstant.separator);
        sb.append(info.getContractId() == null ? "" : info.getContractId()).append(TransBizConstant.separator);
        sb.append(info.getAmortMethodTableId() == null ? "" : info.getAmortMethodTableId()).append(TransBizConstant.separator);
        sb.append(info.getSerialNumber() == null ? "" : info.getSerialNumber()).append(TransBizConstant.separator);
        sb.append(info.getCustomerAccountNumber() == null ? "" : info.getCustomerAccountNumber()).append(TransBizConstant.separator);
        sb.append(info.getLoanUse() == null ? "" : info.getLoanUse()).append(TransBizConstant.separator);
        String firstPaymentDate = info.getFirstPaymentDate() == null ? "" : DateUtils.formatDate(info.getFirstPaymentDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(firstPaymentDate).append(TransBizConstant.separator);
        String lastPaymentDate = info.getLastPaymentDate() == null ? "" : DateUtils.formatDate(info.getLastPaymentDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(lastPaymentDate).append(TransBizConstant.separator);
        sb.append(info.getMemoid() == null ? "" : info.getMemoid()).append(TransBizConstant.separator);
        sb.append( "\r\n");
        fileBean.setInvestorId(loanAccount.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createLoanPlanFileMessage(AmLoanMainInvestorInfo item) {
        List<AmRepaymentPlanInvestorInfo> planInvestorInfoList = extAmRepaymentPlanInvestorInfoMapper.selectByInvestorLoanIdNotPlanStatus
                (item.getInvestorLoanId(), TransBizConstant.LOAN_PLAN_STATUS_9);
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        planInvestorInfoList.forEach(
                info -> {
                    sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
                    sb.append(info.getInvestorId() == null ? "" : info.getInvestorId()).append(TransBizConstant.separator);
                    sb.append(info.getBillingTenor() == null ? "" : info.getBillingTenor()).append(TransBizConstant.separator);
                    sb.append(info.getChannel() == null ? "" : info.getChannel()).append(TransBizConstant.separator);
                    sb.append(info.getProductId() == null ? "" : info.getProductId()).append(TransBizConstant.separator);
                    sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
                    sb.append(info.getPrincipalAmount() == null ? "0.00" : info.getPrincipalAmount().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getInterestAmount() == null ? "0.00" : info.getInterestAmount().toPlainString()).append(TransBizConstant.separator);
                    sb.append(info.getFeeAmount() == null ? "0.00" : info.getFeeAmount().toPlainString()).append(TransBizConstant.separator);
                    String paymentDueDate = info.getPaymentDueDate() == null ? "" : DateUtils.formatDate(info.getPaymentDueDate(), DateUtils.DATE_PATTERN_ANOTHER);
                    sb.append(paymentDueDate).append(TransBizConstant.separator);
                    sb.append( "\r\n");
                }
        );
        fileBean.setInvestorId(item.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createRepayOrderFileMessage(TlPaymentInvestorMapping info) {
        List<TlPaymentAllocationInvestorLog> logList = extTlPaymentAllocationInvestorLogMapper.findByInvestorLoanIdAndSeqNo(info.getInvestorLoanId(), info.getSeqNo());
        if (CollectionUtils.isEmpty(logList)) {
            return null;
        }
        BigDecimal repayPrincipal = logList.stream().filter(acct -> info.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                .filter(acct -> TransBizConstant.ACCOUNT_TYPE_06.equals(acct.getConnectAccountType()))
                .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal repayInterest = logList.stream().filter(acct -> info.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                .filter(acct -> TransBizConstant.ACCOUNT_TYPE_04.equals(acct.getConnectAccountType()))
                .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal repayPenalty = logList.stream().filter(acct -> info.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                .filter(acct -> TransBizConstant.ACCOUNT_TYPE_11.equals(acct.getConnectAccountType()))
                .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal repayComp = logList.stream().filter(acct -> info.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                .filter(acct -> TransBizConstant.ACCOUNT_TYPE_12.equals(acct.getConnectAccountType()))
                .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal repayFee = logList.stream().filter(acct -> info.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                .filter(acct -> TransBizConstant.ACCOUNT_TYPE_03.equals(acct.getConnectAccountType()))
                .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getSeqNo() == null ? "" : info.getSeqNo()).append(TransBizConstant.separator);
        sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
        sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
        String paymentType = logList.get(0).getPaymentType();
        String repayType = "";
        if (TransBizConstant.REPAYMENT_TYPE_PREPAY.equals(paymentType)) {
            repayType = "02";
        } else if (TransBizConstant.REPAYMENT_TYPE_AUTO.equals(paymentType)) {
            repayType = "01";
        } else if (TransBizConstant.REPAYMENT_TYPE_OVERDUE.equals(paymentType)) {
            repayType = "04";
        } else if (TransBizConstant.REPAYMENT_TYPE_PRETERMINTION.equals(paymentType)) {
            repayType = "03";
        } else if (TransBizConstant.REPAYMENT_TYPE_CMPS.equals(paymentType)) {
            repayType = "05";
        } else if (TransBizConstant.REPAYMENT_TYPE_CRT.equals(paymentType)) {
            repayType = "06";
        }
        sb.append(repayType).append(TransBizConstant.separator);
        String repayDate = info.getBusinessDate() == null ? "" : DateUtils.formatDate(info.getBusinessDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(repayDate).append(TransBizConstant.separator);
        sb.append(repayPrincipal == null ? "0.00" : repayPrincipal.toPlainString()).append(TransBizConstant.separator);
        sb.append(repayInterest == null ? "0.00" : repayInterest.toPlainString()).append(TransBizConstant.separator);
        sb.append(repayPenalty == null ? "0.00" : repayPenalty.toPlainString()).append(TransBizConstant.separator);
        sb.append(repayComp == null ? "0.00" : repayComp.toPlainString()).append(TransBizConstant.separator);
        sb.append(repayFee == null ? "0.00" : repayFee.toPlainString()).append(TransBizConstant.separator);
        sb.append( "\r\n");
        fileBean.setInvestorId(info.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createRepayPlanFileMessage(TlPaymentInvestorMapping item) {
        List<TlPaymentAllocationInvestorLog> logAllList = extTlPaymentAllocationInvestorLogMapper.findByInvestorLoanIdAndSeqNo(item.getInvestorLoanId(), item.getSeqNo());
        if (CollectionUtils.isEmpty(logAllList)) {
            return null;
        }
        Map<Integer, List<TlPaymentAllocationInvestorLog>> logMap = logAllList.stream()
                .collect(Collectors.groupingBy(TlPaymentAllocationInvestorLog::getBillingTenor));
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        logMap.forEach(
                (k, logList) -> {
                    BigDecimal repayPrincipal = logList.stream().filter(acct -> item.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                            .filter(acct -> TransBizConstant.ACCOUNT_TYPE_06.equals(acct.getConnectAccountType()))
                            .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal repayInterest = logList.stream().filter(acct -> item.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                            .filter(acct -> TransBizConstant.ACCOUNT_TYPE_04.equals(acct.getConnectAccountType()))
                            .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal repayPenalty = logList.stream().filter(acct -> item.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                            .filter(acct -> TransBizConstant.ACCOUNT_TYPE_11.equals(acct.getConnectAccountType()))
                            .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal repayComp = logList.stream().filter(acct -> item.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                            .filter(acct -> TransBizConstant.ACCOUNT_TYPE_12.equals(acct.getConnectAccountType()))
                            .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal repayFee = logList.stream().filter(acct -> item.getInvestorLoanId().equals(acct.getInvestorLoanId()))
                            .filter(acct -> TransBizConstant.ACCOUNT_TYPE_03.equals(acct.getConnectAccountType()))
                            .map(TlPaymentAllocationInvestorLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    sb.append(item.getSeqNo() == null ? "" : item.getSeqNo()).append(TransBizConstant.separator);
                    sb.append(item.getCustomerId() == null ? "" : item.getCustomerId()).append(TransBizConstant.separator);
                    sb.append(item.getInvestorLoanId() == null ? "" : item.getInvestorLoanId()).append(TransBizConstant.separator);
                    sb.append(k).append(TransBizConstant.separator);
                    String paymentType = logAllList.get(0).getPaymentType();
                    String repayType = "";
                    if (TransBizConstant.REPAYMENT_TYPE_PREPAY.equals(paymentType)) {
                        repayType = "02";
                    } else if (TransBizConstant.REPAYMENT_TYPE_AUTO.equals(paymentType)) {
                        repayType = "01";
                    } else if (TransBizConstant.REPAYMENT_TYPE_OVERDUE.equals(paymentType)) {
                        repayType = "04";
                    } else if (TransBizConstant.REPAYMENT_TYPE_PRETERMINTION.equals(paymentType)) {
                        repayType = "03";
                    } else if (TransBizConstant.REPAYMENT_TYPE_CMPS.equals(paymentType)) {
                        repayType = "05";
                    } else if (TransBizConstant.REPAYMENT_TYPE_CRT.equals(paymentType)) {
                        repayType = "06";
                    }
                    sb.append(repayType).append(TransBizConstant.separator);
                    String repayDate = item.getBusinessDate() == null ? "" : DateUtils.formatDate(item.getBusinessDate(), DateUtils.DATE_PATTERN_ANOTHER);
                    sb.append(repayDate).append(TransBizConstant.separator);
                    sb.append(repayPrincipal == null ? "0.00" : repayPrincipal.toPlainString()).append(TransBizConstant.separator);
                    sb.append(repayInterest == null ? "0.00" : repayInterest.toPlainString()).append(TransBizConstant.separator);
                    sb.append(repayPenalty == null ? "0.00" : repayPenalty.toPlainString()).append(TransBizConstant.separator);
                    sb.append(repayComp == null ? "0.00" : repayComp.toPlainString()).append(TransBizConstant.separator);
                    sb.append(repayFee == null ? "0.00" : repayFee.toPlainString()).append(TransBizConstant.separator);
                    sb.append( "\r\n");
                }
        );

        fileBean.setInvestorId(item.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createTransferFileMessage(TlInvestorLoanChange info) {
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
        sb.append(info.getChannel() == null ? "" : info.getChannel()).append(TransBizConstant.separator);
        sb.append(info.getBusinessDate() == null ? "" : info.getBusinessDate()).append(TransBizConstant.separator);
        sb.append(info.getSeqNo() == null ? "" : info.getSeqNo()).append(TransBizConstant.separator);
        sb.append(info.getEventCode() == null ? "" : info.getEventCode()).append(TransBizConstant.separator);
        sb.append(info.getBeforeStatus() == null ? "" : info.getBeforeStatus()).append(TransBizConstant.separator);
        sb.append(info.getAfterStatus() == null ? "" : info.getAfterStatus()).append(TransBizConstant.separator);
        sb.append(info.getPrincipalAmount() == null ? "" : info.getPrincipalAmount().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getInterestAmount() == null ? "" : info.getInterestAmount().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getPenaltyInterestAmount() == null ? "" : info.getPenaltyInterestAmount().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getCompoundInterestAmount() == null ? "" : info.getCompoundInterestAmount().toPlainString()).append(TransBizConstant.separator);
        sb.append( "\r\n");
        fileBean.setInvestorId(info.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

    @Override
    public FileBean createIntrFileMessage(TlInvestorInterestAccrualLog info) {
        FileBean fileBean = new FileBean();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getCustomerId() == null ? "" : info.getCustomerId()).append(TransBizConstant.separator);
        sb.append(info.getInvestorLoanId() == null ? "" : info.getInvestorLoanId()).append(TransBizConstant.separator);
        sb.append(info.getBillingTenor() == null ? "" : info.getBillingTenor()).append(TransBizConstant.separator);
        String accrualDate = info.getAccrualDate() == null ? "" : DateUtils.formatDate(info.getAccrualDate(), DateUtils.DATE_PATTERN_ANOTHER);
        sb.append(accrualDate).append(TransBizConstant.separator);
        sb.append(info.getAccountType() == null ? "" : info.getAccountType()).append(TransBizConstant.separator);
        AmInterestAccrualInvestorInfo acct = extAmInterestAccrualInvestorInfoMapper.findByAccountId(info.getAccountId());
        sb.append(acct.getNonAccrualsFlag() == null ? "" : acct.getNonAccrualsFlag()).append(TransBizConstant.separator);
        sb.append(info.getDailyRate() == null ? "" : info.getDailyRate().toPlainString()).append(TransBizConstant.separator);
        sb.append(info.getAccrualProvision() == null ? "" : info.getAccrualProvision().toPlainString()).append(TransBizConstant.separator);
        sb.append( "\r\n");
        fileBean.setInvestorId(info.getInvestorId());
        fileBean.setFileString(sb.toString());
        return fileBean;
    }

}
